package
{
	import com.up360.rapidcalc.box.BoxManager;
	import com.up360.rapidcalc.common.events.RapidCalcEvent;
	import com.up360.rapidcalc.external.FromExternal;
	import com.up360.rapidcalc.external.ToExternal;
	import com.up360.rapidcalc.global.Config;
	import com.up360.rapidcalc.global.LayerManager;
	import com.up360.rapidcalc.ui.GameLoading;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.external.ExternalInterface;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import flash.system.Security;
	
	
	/**
	 * Author:郎福涛
	 * Date: May 2, 2013 9:43:57 AM
	 * Synopsis:
	 */
	[SWF(width="876",height="600")]
	public class RapidCalcMain extends Sprite
	{
		private var _gameLoader:Loader;
		private var _gameObject:Object;
		private var _gameLoading:GameLoading;
		private var _configurationLoader:URLLoader;
		private var _configurationData:String;
		
		public function RapidCalcMain()
		{
			Security.allowDomain("*");
			_gameLoading=new GameLoading();
			LayerManager.initialize(this);
			Config.instance.initialize(this.loaderInfo,true,configComplete);
		}
		
		private function configComplete():void
		{
			FromExternal.initialize();
			if(loaderInfo.parameters.type=="2"){
				if(ExternalInterface.available){
					ExternalInterface.addCallback("setConfig",setConfig);
				}
			}else{
				loadConfiguration();
			}
			ToExternal.initComplete();
		}
		public function setConfig(data:String):void{
			_configurationData=data;
			loadGame();
		}
		private function loadConfiguration():void{
			var b_vars:URLVariables=new URLVariables();
			b_vars.userId=loaderInfo.parameters.userId;
			b_vars.contCpId=loaderInfo.parameters.contCpId;
			var b_url:String=Config.instance.configData.dataInterface[0].getConfig.@url;
			
			var b_request:URLRequest=new URLRequest(b_url);
			b_request.method="POST";
			b_request.data=b_vars;
			
			_configurationLoader=new URLLoader();
			_configurationLoader.addEventListener(Event.COMPLETE,configurationComplete);
			_configurationLoader.load(b_request);
		}
		
		private function configurationComplete(e:Event):void
		{
			_configurationData=_configurationLoader.data;
			loadGame();
		}
		private function loadGame():void{
			_gameLoader=new Loader();
			_gameLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,gameLoaderProgress);
			_gameLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,gameLoaderComplete);
			_gameLoader.load(new URLRequest(loaderInfo.parameters.gameURL));
			addChild(_gameLoading);
		}
		
		private function gameLoaderProgress(e:ProgressEvent):void
		{
			_gameLoading.persent=e.bytesLoaded/e.bytesTotal*100;
		}
		
		private function gameLoaderComplete(e:Event):void
		{
			if(_gameLoading.parent) _gameLoading.parent.removeChild(_gameLoading);
			
			_gameLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,gameLoaderProgress);
			_gameLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE,gameLoaderComplete);
			
			_gameObject=_gameLoader.content as Object;
			_gameObject.addEventListener(RapidCalcEvent.ALERT,alertRC);
			var b_parameters:Object=loaderInfo.parameters;
			var b_obj:Object={};
			b_obj.type=b_parameters.type;//字符串类型，1为正式速算，2为预览，3为情景课堂，情景课堂不保存数据
			b_obj.config=_configurationData;//配置器信息,json格式的字符串
			b_obj.userId=b_parameters.userId;//用户id
			b_obj.contCpId=b_parameters.contCpId;//关卡Id,在预览时为规则Id
			if(loaderInfo.parameters.type=="2"){//预览
				b_obj.subjectDataURL=Config.instance.configData.dataInterface[0].getPreviewQuestion.@url;//读取题目数据的地址
			}else{//正常
				b_obj.subjectDataURL=Config.instance.configData.dataInterface[0].getQuestion.@url;//读取题目数据的地址
			}
			b_obj.saveResultURL=Config.instance.configData.dataInterface[0].saveResult[0].@url;//保存结果的地址
			b_obj.getTop=Config.instance.configData.dataInterface[0].getTop[0].@url;//获取排行榜数据的地址
			
			_gameObject.playGame(b_obj);
			addChild(_gameObject as DisplayObject);
		}
		
		private function alertRC(e:RapidCalcEvent):void{
			var b_title:String=e.param.title;
			var b_msg:String=e.param.msg;
			if(b_title=="" || b_title==null){
				b_title="提示";
			}
			if(b_msg=="" || b_msg==null){
				b_msg="";
			}
			b_msg='<p align="center">'+b_msg+'</p>'
			BoxManager.alert(new AlertBoxMC,b_title,b_msg);
		}
	}
}